package com.prs.crm.action.sys;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.sys.CodeInvoiceService;

/***
 * 单据编码
 */
public class CodeInvocieAction extends BaseAction {

	private static final long serialVersionUID = 3878978960776455327L;

	private CodeInvoice codeInvoice;

	private CodeInvoiceService codeInvoiceService;
	
	private String encodes;

	/**
	 * 判断默认
	 */
	private String codemoren;
	/**
	 * 编码前缀
	 * 
	 * @return
	 */
	private String codeFirst;
	/**
	 * 流水位数
	 * 
	 * @return
	 */
	private String liuShui;
	/**
	 * 起始流水号
	 * 
	 * @return
	 */
	private String liuShuiNum;
	/**
	 * 年位数
	 */
	private String yearWei;
	/**
	 * 编码方式
	 * 
	 * @return
	 */
	private String codeWay;
	
	private PaginationSupport pagination;

	@Action("editCodeInvoice")
	public String editCodeInvoice() {
		return SUCCESS;
	}
	@Action("editCode2")
	public String editCode2() {

		return SUCCESS;
	}
	@Action("listCodeInvoice")
	public String listCodeInvoice() {
		 DetachedCriteria criteria =
		 this.getPaginationHelper().createQuery(CodeInvoice.class);
		 pagination = this.getPaginationHelper().prepare(pagination);
		 this.setPagination(this.getCodeInvoiceService().listByPage(criteria,
		 pagination));
		return SUCCESS;
	}

	@Action(value = "saveCodeInvoice", results = {
			@Result(name = "success", type = "redirect", location = "editCode2"),
			@Result(name = "error", type = "chain", location = "editCode2")			
	})
	public String saveCodeInvoice() {
		if(liuShui==null||liuShui.equals("")){
			this.addActionError("流水号位数 不能为空");
			return ERROR;
		}
		if(liuShuiNum==null||liuShuiNum.equals("")){
			this.addActionError("起始流水号不能为空");
			return ERROR;
		}
		if(codeInvoice.getCodePlan()==null||codeInvoice.getCodePlan().equals("")){
			this.addActionError("方案名称不能为空");
			return ERROR;
		}
		int a=0;
		try {
			a=Integer.parseInt(liuShui);
		} catch (Exception e) {
			this.addActionError("流水号位数 只能为数字");
			return ERROR;
		}
		try {
			Integer.parseInt(liuShuiNum);
		} catch (Exception e) {
			this.addActionError("起始流水号 只能为数字");
			return ERROR;
		}
		if(liuShuiNum.length()>a){
			this.addActionError("起始流水号 的位数大于流水号位数");
			return ERROR;
		}
		int EncodeRepl=this.getCodeInvoiceService().getInvoiceCodeName(codeInvoice.getCodePlan());
		if(EncodeRepl!=0){
		this.setPrompt("该方案名称已经存在!");
		return ERROR;
		
		}else{
		
		/**
		 * 是否默认
		 */
		if ("是".equals(codemoren)) {
			codeInvoice.setCodeFirst(codeFirst);
			codeInvoice.setYndefault("是");
		} else if ("否".equals(codemoren)) {
			codeInvoice.setCodeFirst("");
			codeInvoice.setYndefault("否");
		}
		/**
		 * 起始流水号
		 */
		codeInvoice.setLiuShuiNumScreen(liuShui);
		int digitValue = Integer.parseInt(liuShui);
		if (digitValue != liuShuiNum.length()) {
			String OneNum = appendEke(digitValue - liuShuiNum.length());
			codeInvoice.setLiuShuiNum(OneNum + liuShuiNum);
			codeInvoice.setNumBackUp(OneNum + liuShuiNum);
		} else {
			codeInvoice.setLiuShuiNum(liuShuiNum);
			codeInvoice.setNumBackUp(liuShuiNum);
		}
		/**
		 * 编码方式
		 */
		int yearsj = 4;
		if ("2位".equals(yearWei)) {
			yearsj = 2;
		}
		if ("年编号".equals(codeWay)) {
			String years = "";
			if (yearsj == 2) {
				years = calendarP("yy");
			} else {
				years = calendarP("yyyy");
			}
			codeInvoice.setCodeWayScreen("年编号");
			codeInvoice.setCodeWay(years.trim());
		} else if ("月编号".equals(codeWay)) {
			String years = "";
			if (yearsj == 2) {
				years = calendarP("yyMM");
			} else {
				years = calendarP("yyyyMM");
			}
			codeInvoice.setCodeWayScreen("月编号");
			codeInvoice.setCodeWay(years.trim());
		} else if ("日编号".equals(codeWay)) {
			String years = "";
			if (yearsj == 2) {
				years = calendarP("yyMMdd");
			} else {
				years = calendarP("yyyyMMdd");
			}
			codeInvoice.setCodeWayScreen("日编号");
			codeInvoice.setCodeWay(years.trim());
		}
		this.getCodeInvoiceService().save(codeInvoice);
		this.setPrompt("成功產生方案!");
		return SUCCESS;
		}
	}

	@Action(value = "Encodeing", results = { @Result(name = "success", type = "json", params = {
			"root", "encodes" }) })
			
	public String validateStockAlert1() {
		CodeInvoice VObject=this.getCodeInvoiceService().get(codeInvoice.getId());			
		encodes=EnCoding(VObject);	
		return SUCCESS;
	}
	
	@Action("viewCodeInvoice")
	public String viewCodeInvoice() {
		codeInvoice = this.getCodeInvoiceService().get(codeInvoice.getId());
		return SUCCESS;
	}

	
	private static String EnCoding(CodeInvoice codeInvoice){
		String Encodings=codeInvoice.getCodeFirst()+codeInvoice.getCodeWay()+codeInvoice.getNumBackUp();
		int numbackUp=Integer.parseInt(codeInvoice.getNumBackUp());
		int codeNumber=Integer.parseInt(codeInvoice.getLiuShuiNumScreen());
		String a="";
		for(int i=0;i<codeNumber;i++){
			a=a+"9";
		}
		int num=Integer.parseInt(a);
		if(num<=numbackUp){
			Encodings=Encodings+",1";
		}else{
			Encodings=Encodings+",0";
		}
		return Encodings.trim();
	}
	
	private static String calendarP(String st) {
		Calendar cal = Calendar.getInstance();
		String yestedayDate = new SimpleDateFormat(st).format(cal.getTime());
		return yestedayDate;
	}

	private static String appendEke(int num) {
		String returnV = "";
		for (int i = 0; i < num; i++) {
			returnV = returnV + "0";
		}
		return returnV;
	}

	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public String getCodemoren() {
		return codemoren;
	}

	public void setCodemoren(String codemoren) {
		this.codemoren = codemoren;
	}

	public String getCodeFirst() {
		return codeFirst;
	}

	public void setCodeFirst(String codeFirst) {
		this.codeFirst = codeFirst;
	}

	public String getLiuShui() {
		return liuShui;
	}

	public void setLiuShui(String liuShui) {
		this.liuShui = liuShui;
	}

	public String getLiuShuiNum() {
		return liuShuiNum;
	}

	public void setLiuShuiNum(String liuShuiNum) {
		this.liuShuiNum = liuShuiNum;
	}

	public String getYearWei() {
		return yearWei;
	}

	public void setYearWei(String yearWei) {
		this.yearWei = yearWei;
	}

	public String getCodeWay() {
		return codeWay;
	}

	public void setCodeWay(String codeWay) {
		this.codeWay = codeWay;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String getEncodes() {
		return encodes;
	}

	public void setEncodes(String encodes) {
		this.encodes = encodes;
	}

}
